$(function () {
    $("#jqGrid").Grid({
        url: '../user/list',
        colModel: [{
            label: 'id', name: 'id', index: 'id', key: true, hidden: true
        }, {
            label: '会员名称', name: 'username', index: 'username',align:'center', width: 70
        }, {
            label: '会员密码', name: 'password', index: 'password', hidden: true
        }, {
            label: '性别', name: 'gender', index: 'gender', align:'center',width: 20, formatter: function (value) {
                return transGender(value);
            }
        }, {
            label: '出生日期', name: 'birthday', index: 'birthday',align:'center', width: 70, formatter: function (value) {
                return transDate(value);
            },hidden: true
        }, {
            label: '注册时间', name: 'registerTime', index: 'register_time',align:'center', width: 70, formatter: function (value) {
                return transDate(value);
            }
        }, {
            label: '最后登录时间', name: 'lastLoginTime', index: 'last_login_time',align:'center', width: 70, formatter: function (value) {
                return transDate(value);
            },hidden: true
        }, {
            label: '最后登录Ip', name: 'lastLoginIp', index: 'last_login_ip', hidden: true
        }, {
            label: '会员等级', name: 'levelName',align:'center', width: 40
        }, {
            label: '用户名称', name: 'nickname', index: 'nickname',align:'center', width: 70
        }, {
            label: '手机号码', name: 'mobile', index: 'mobile',align:'center', width: 50
        },  {
            label: '真实姓名', name: 'realname', index: 'realname',align:'center', width: 50
        },  {
            label: '身份证号', name: 'idcard', index: 'idcard',align:'center', width: 60
        }, {
            label: '注册Ip', name: 'registerIp', index: 'register_ip', hidden: true
        }, {
            label: '头像', name: 'avatar', index: 'avatar', align:'center',width: 40, formatter: function (value) {
                return transImg(value);
            },hidden: true
        }, {
            label: '微信Id', name: 'weixinOpenid', index: 'weixin_openid', width: 80, hidden: true
        },{
            label: '笑笑购物劵', name: 'balanceFee', index: 'balance_fee', width: 50,align:'center'
        },{
            label: '签到购物劵', name: 'signFee', index: 'sign_fee', width: 50,align:'center'
        },{
            label: '上次签到时间', name: 'signTime', index: 'sign_time',align:'center', width: 70, formatter: function (value) {
                return transDate(value);
            }
        },]
    });
    var newHeight = $(window).height()-130;
    $(".ui-jqgrid .ui-jqgrid-bdiv").css("cssText","height: "+newHeight+"px!important;");
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        user: {
            gender: 1
        },
        userAuth:null,
        ruleValidate: {
            username: [
                {required: true, message: '会员名称不能为空', trigger: 'blur'}
            ],
            nickname: [
                {required: true, message: '用户名称不能为空', trigger: 'blur'}
            ],
            realname: [
                {required: true, message: '用户姓名不能为空', trigger: 'blur'}
            ],
            mobile: [
                {required: true, message: '手机号码不能为空', trigger: 'blur'},
                { type: 'string',pattern:/^1[345789]\d{9}$/, message:'请输入格式正确的手机号码', trigger:'blur'}
            ],
            idcard: [
                {required: true, message: '身份证号不能为空', trigger: 'blur'},
                { type: 'string',pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message:'请输入格式正确的身份证号码', trigger:'blur'}
            ],
            balanceFee: [
                { type: 'string',pattern:/^([1-9]\d{0,9}|0)([.]?|(\.\d{1,3})?)$/, message:'金额的输入格式不正确,请确认!', trigger:'blur'}
            ],
            signFee: [
                { type: 'string',pattern:/^([1-9]\d{0,9}|0)([.]?|(\.\d{1,3})?)$/, message:'金额的输入格式不正确,请确认!', trigger:'blur'}
            ]
        },
        q: {
            username: '',
            mobile:'',
            realname:'',
            idcard:'',
            userLevelId:'',
        },
        userLevels: []
    },
    mounted(){
        this.getUserLevels();
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.user = {gender: '1'};
            vm.userLevels = [];

            this.getUserLevels();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
            this.getUserLevels();
        },
        saveOrUpdate: function (event) {
            var url = vm.user.id == null ? "../user/save" : "../user/update";

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.user),
                successCallback: function (r) {
                    alert(r.errmsg, function (index) {
                        if(r.errno==0){
                            vm.reload();
                        }
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../user/delete",
                    contentType: "application/json",
                    params: JSON.stringify(ids),
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });

            });
        },
        exportUser: function () {
            exportFile('#rrapp', '../user/export', {'username': vm.q.username});
        },
        coupon: function () {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            openWindow({
                title: '优惠券',
                type: 2,
                content: '../shop/usercoupon.html?userId=' + id
            })
        },
        address: function () {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            openWindow({
                title: '收获地址',
                type: 2,
                content: '../shop/address.html?userId=' + id
            })
        },
        shopCart: function () {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            openWindow({
                title: '购物车',
                type: 2,
                content: '../shop/cart.html?userId=' + id
            })
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../user/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.user = r.user;
                    if(r.user.userAuthEntity){
                        vm.userAuth = r.user.userAuthEntity;
                    }else{
                        vm.userAuth = null;
                    }
                }
            });
        },
        /**
         * 获取会员级别
         */
        getUserLevels: function () {
            Ajax.request({
                url: "../userlevel/queryAll",
                async: true,
                successCallback: function (r) {
                    vm.userLevels = r.list;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'username': vm.q.username,
                    'mobile': vm.q.mobile,
                    'realname': vm.q.realname,
                    'idcard': vm.q.idcard,
                    'userLevelId': vm.q.userLevelId,
                },
                page: page,
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});
